﻿using System.Linq;
using System.Web.Mvc;
using JStore.Core.Entities;
using JStore.Tasks;
using JStore.Web.Controllers.SysManage.ViewModels;
using SpeadFrm.Mappers;

namespace JStore.Web.Controllers.Home
{
    public class HomeController : BaseController
    {
        private readonly UserPasswordTask _userPasswordTask = new UserPasswordTask();
        private readonly RoleMenuTask _roleMenuTask = new RoleMenuTask();
        private readonly MenusTask _menusTask = new MenusTask();

        public ActionResult Index()
        {
            var userPassword = _userPasswordTask.GetByUserName(LogOnUserName);
            var menuIds = _roleMenuTask.GetListByRoleId(userPassword.RoleId)
                .Select(c => c.MenuId).ToList();
            ViewData["MenuList"] = _menusTask.GetListByIds(menuIds);
            return View();
        }

        public ActionResult Menu()
        {
            var userPassword = _userPasswordTask.GetByUserName(LogOnUserName);
            var menus = _roleMenuTask.GetMenuLstByRoleId(userPassword.RoleId)
                .Select(EntityMapper.Map<Menus, MenuModel>)
                .OrderBy(c => c.SortOrder)
                .ToList();
            return View(menus);
        }
    }
}